//
//  YXStatusTopView.swift
//  微博YX
//
//  Created by apple on 15/11/27.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit
import SDWebImage

class YXStatusTopView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //有一个微博模型数据
    var status: YXStatus? {
        didSet{
            //            添加子控件，给子控件赋值
            if  let profile = status?.user?.profile_image_url {
                iconImage.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar"))
            }
            //            用户名
            userNameLabel.text = status?.user?.screen_name
            //            发表时间
            timeLabel.text = status?.created_at
            //            来源
            sourceLabel.text = status?.source
            //            会员
            if let vip = status?.user?.mbrank {
                if vip > 0 && vip < 7 {
                    vipImage.image = UIImage(named: "common_icon_membership_level\(vip)")
                }
            }else{
                //                不是会员清空图标
                vipImage.image = nil
            }
            
            //            认证等级
            if let verified = status?.user?.verified_type {
                switch verified {
                case 0:
                    verifiedImage.image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    verifiedImage.image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    verifiedImage.image = UIImage(named: "avatar_grassroot")
                default:
                    verifiedImage.image = nil
                    
                }
            }
            
            
            
        }
    }
    //    准备ui
    func prepareUI(){
        //        添加子控件
        addSubview(separatorView)
        addSubview(iconImage)
        addSubview(userNameLabel)
        addSubview(timeLabel)
        addSubview(vipImage)
        addSubview(verifiedImage)
        addSubview(sourceLabel)
        //        添加约束
//        顶部的分隔线的view
        separatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
        //        头像
        iconImage.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView:separatorView, size: CGSizeMake(30, 30), offset: CGPoint(x: 8, y: 8))
        //        用户名
        userNameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconImage, size: nil, offset: CGPoint(x: 8, y: 0))
        //        发表时间
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconImage, size: nil, offset: CGPoint(x: 8, y: 0))
        //        vip 图标
        vipImage.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: userNameLabel, size: CGSizeMake(14, 14), offset: CGPoint(x: 8, y: 0))
        /// 认证图标
        verifiedImage.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconImage, size: CGSize(width: 15, height: 15), offset: CGPoint(x: 8, y: 8))
        //        来源
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: 8, y: 0))
        
    }
    
    
    // 懒加载
//    添加分隔线
    private lazy var separatorView: UIView = {
        let spView = UIView()
        spView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        return spView
    }()
    //    头像
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: ""))
    
    //    id
    private lazy var userNameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fonSize: 14)
    
    //    时间
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fonSize: 10)
    
    //   vip 图标
    private lazy var vipImage: UIImageView = UIImageView(image: UIImage(named: ""))
    
    //    认证图标
    private lazy var verifiedImage: UIImageView = UIImageView(image: UIImage(named: ""))
    //    来源
    private lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fonSize: 10)
 
}
